package com.lin.demo.redission;

import com.lin.demo.MainApplication;
import com.lin.demo.redission.entry.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.runner.RunWith;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName RMapCacheOpertions.java
 * @Description 带淘汰机制的hash表操作
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class)
public class RMapCacheOpertions {

    @Autowired
    RedissonClient redissonClient;

    public void rMapCache() {

        //获得hash表，这里RMapOpertions为主key
        RMapCache<String, User> userInfo = redissonClient.getMapCache("userInfo");
        User userA = User.builder().id("1").name("嬴政").age(22).address("秦朝").build();
        User userB = User.builder().id("2").name("李斯").age(22).address("秦朝").build();
        User userC = User.builder().id("3").name("孙悟空").age(22).address("唐").build();
        //添加元素，返回的值为之前hash表中的值，并且为每个子元素添加过期时间
        userInfo.put(userA.getId(), userA, 20, TimeUnit.SECONDS);
        userInfo.put(userB.getId(), userB, 20, TimeUnit.SECONDS);
        userInfo.put(userC.getId(), userC, 20, TimeUnit.SECONDS);

        //获得userInfo中所有的key
        Set<String> keySet = userInfo.readAllKeySet();
        log.info("获得userInfo中所有的key:{}", keySet.toString());

        //获得userInfo中所有的values
        Collection<User> users = userInfo.readAllValues();
        log.info("获得userInfo中所有的值:{}", users.toString());

        //获得userInfo中所有的元素对象
        Set<Map.Entry<String, User>> entries = userInfo.readAllEntrySet();
        log.info("获得userInfo中所有的元素对象:{}", entries.toString());
        userInfo.clear();

        //其他操作与RMap类似，这里就不再操作

    }
}
